<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_model extends MY_Model
{
public function userList($data = array(),$per_page,$limit){
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND u.addtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $where .= " AND u.addtime <= {$data['endtime']}";
        }
        if(!empty($data['name'])){
            $where .= " AND u.truename like '%{$data['truename']}%'";
        }
        if(!empty($data['usernames'])){
            $where .= " AND u.username in ({$data['usernames']})";
        }
     
        $limit = "LIMIT {$per_page},{$limit}";
       $sql = "SELECT
                    u.*,d.name department_name
            FROM
                   tr_user u,tr_department d  
            where 
                u.departmentid=d.id and u.status<>2 {$where} ORDER BY u.addtime desc {$limit} ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }


    public function get_user_cnt($data = array()){
        $where = " WHERE 1 ";
        if(!empty($data['starttime'])){
            $where .= " AND u.addtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $where .= " AND u.addtime <= {$data['endtime']}";
        }
        if(!empty($data['name'])){
            $where .= " AND u.truename like '%{$data['truename']}%'";
        }
        $sql = "SELECT
                    u.*,d.name department_name
            FROM
                   tr_user u,tr_department d  
            where 
                u.departmentid=d.id and u.status<>2 ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }

    //编辑状态
    public function edit_status($id,$status){
        $this->db->where("id",$id);
        $data = array(
            'status'=>$status
        );
        $res = $this->db->update("bp_feedback",$data);
        return $res;
    }





/**
 * 修改用户停用启用状态
 * @param  [type] $table [description]
 * @param  array  $data  [description]
 * @return [type]        [description]
 */
   public function editStatus($table,$data = array()){
        $this->db->where("AccountID",$data['id']);
        $datas = array(
            'Status'=>$data['Status']
        );

        $res = $this->db->update($table,$datas);
        return $res;
    }  

  public function editUser($AccountID = 0, $data = [])
    {
        if($AccountID){
             //修改
            $this->db->where("AccountID",$AccountID);
            $res = $this->db->update("tr_user", $data);
        }else{
            $checkusername = $this->check_username($data['username']);
            if($checkusername)return false ;

            $this->db->insert("tr_user", $data);
            $res = $this->db->insert_id();
        }
        return $res;
    } 


    public function check_username($username)
    {
        $sql = "SELECT * FROM tr_user WHERE username = '$username'";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }
    /**
     * 获取部门列表
     */
    public function getDepartentList(){
        $this->db->order_by('sort','desc');
        $query = $this->db->get("tr_department");
        return $query->result_array();
    }

}


